﻿using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using HelloWorldWindowsPhone;
using Microsoft.Phone.Controls.Maps.Core;
using System.ComponentModel;

namespace WindowsPhoneApplication1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        public PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            //VerifyPropertyName(propertyName);
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }

        private GeoCoordinate mapCenter;
        public GeoCoordinate Center { 
            get { return mapCenter; }
            set {
                if (value == mapCenter)
                    return;
                mapCenter = value;
                //NotifyPropertyChanged("Center");
            } 
        }
        static private GeoCoordinate DefaultLocation = new GeoCoordinate(31.200833, 121.429726);

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Retrieve the center of the current map view.
            mapCenter = map1.Center;

            // Create a pushpin to put at the center of the view.
            Pushpin pin1 = new Pushpin();
            pin1.Location = mapCenter;
            map1.Children.Add(pin1);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {
            MapTileLayer bingtileLayer = new MapTileLayer();            
            bingtileLayer.Width = 431;            
            bingtileLayer.Height = 540;            
            GeoCoordinate center = new GeoCoordinate(36,104);            
            TileSource tileSource = new BingChinaTitleSource();            
            bingtileLayer.TileSources.Add(tileSource);            
            bingtileLayer.Opacity = 0.9;
            map1.Children.Add(bingtileLayer);
            map1.Mode = new MercatorMode();
            map1.Center = center;
            centerLocation();
        }

        private void centerLocation()
        {
            map1.Center = DefaultLocation;
            map1.ZoomLevel = 16;
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            centerLocation();
        }
    }
}